package com.hc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.hc.bean.BSizeBean;
import com.hc.service.GradeConfigService;
import com.hc.serviceImpl.GradeConfigServiceImpl;

public class GradeConfigServlet extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}


@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	String actionType  = req.getParameter("actionType");
	
	if("updateConfig".equals(actionType)){
		
		updateGradeConfig(req, resp);
	}else if("delConfig".equals(actionType)){
		
		delGradeConfig(req, resp);
	}else if("addConfig".equals(actionType)){
		
		addGradeConfig(req, resp);
		
	}else if("getConfigs".equals(actionType)){
		
		getGradeConfig(req, resp);
	}
	
	
	
		
	
	}

	
	private void getGradeConfig(HttpServletRequest req, HttpServletResponse resp) {
		
		GradeConfigService service = new GradeConfigServiceImpl();
		
		List<BSizeBean> list = service.getConfigs();
		String msg = "";
		if(list!=null&&list.size()!=0){
				
				JSONArray jsonArray2 = JSONArray.fromObject( list );
				msg =jsonArray2.toString();
				
			}else{
				
				msg = "false";
			}
		returnMsg(req, resp, msg);
	}
	
	
	private void addGradeConfig(HttpServletRequest req, HttpServletResponse resp) {
		
		String name  = req.getParameter("name").trim();
		String maxNumberStr = req.getParameter("maxNumber").trim();
		String minNumberStr  = req.getParameter("minNumber").trim();
		String id  = req.getParameter("id").trim();
		
		String msg="";
		try {
			double maxNumber = Double.parseDouble(maxNumberStr);
			double minNumber = Double.parseDouble(minNumberStr);
		
			if(maxNumber>minNumber){
				BSizeBean bean = new BSizeBean();
				bean.setId(Long.parseLong(id));
				bean.setName(name);
				bean.setMaxSpeed(maxNumber);
				bean.setMinSpeed(minNumber);
				
				GradeConfigService service = new GradeConfigServiceImpl();
				msg = 	service.addConfig(bean)+"";
				
				
		}else{
				
				msg= "{\"msg\": \"最大值不能等于或小于最小值\"}";
			}
			
		} catch (Exception e) {
			msg= "{\"msg\": \"最大值与最小值只能为数字,不能添加其他字符\"}";
	
		}

	
		returnMsg(req, resp, msg);
		
		
	}
	
	
	private void delGradeConfig(HttpServletRequest req, HttpServletResponse resp) {
		String id  = req.getParameter("id");
		GradeConfigService service = new GradeConfigServiceImpl();
	
		
		returnMsg(req, resp, service.delConfigBean(Long.parseLong(id))+"");
		
	}
	
	
	private void updateGradeConfig(HttpServletRequest req, HttpServletResponse resp) {
		
		
		String name  = req.getParameter("name").trim();
		String maxNumberStr  = req.getParameter("maxNumber").trim();
		String minNumberStr  = req.getParameter("minNumber").trim();
		String id  = req.getParameter("id");
		
		String msg="";
	
		try {
			double maxNumber = Double.parseDouble(maxNumberStr);
			double minNumber = Double.parseDouble(minNumberStr);
		
			if(maxNumber>minNumber){
				BSizeBean bean = new BSizeBean();
				bean.setId(Long.parseLong(id));
				bean.setName(name);
				bean.setMaxSpeed(maxNumber);
				bean.setMinSpeed(minNumber);
				
				GradeConfigService service = new GradeConfigServiceImpl();
				msg = 	service.updateConfig(bean)+"";
				
			}else{
				
				msg= "{\"msg\": \"最大值不能等于或小于最小值\"}";
			}
			
		} catch (Exception e) {
			msg= "{\"msg\": \"最大值与最小值只能为数字,不能添加其他字符\"}";
	
		}

		
		
		returnMsg(req, resp, msg);
		
	}
	
	private void returnMsg(HttpServletRequest req, HttpServletResponse resp,
			String msg) {
		resp.setCharacterEncoding("utf-8");

		PrintWriter w;
		try {
			w = resp.getWriter();
			w.write(msg);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	
	}
}
